#delimit;
set logtype text;
capture log close zip_level_weather;

** REPLACE FILE PATH WITH PATH TO RELEVANT REPLICATION FILES;
local fileloc = "~/KMS_REPLICATION";
log using `fileloc'/log_files/master_dofile.txt, name(master_dofile) replace;

set more off;
clear all;

** Master do-file for replication of Knittel, Miller, and Sanders (hereafter KMS);
** Review of Economics & Statistics;

** Notes: running this do file will rebuild, from raw data sets, all data used in KMS. It will then reproduce all tables and figures present in KMS. Please note that birth data come from restricted data files, which require permission for use and as such are not available on the ReSTAT Dataverse system.

** Master do-file assumes a folder called "REPLICATION" exists on the users drive, with the following folders and subfolders contained within:
	** data (for storage of existing raw data and sets created by the do-file)
		** birth_data
		** emissions_data
		** location_data
		** traffic_data
		** weather_data	
	** regs (for regression output)
	** graphs (for figure output)
	** log_files (for text-formatted log files)
	** simulation (for Monte Carlo simulation portion)


** DATA CONSTRUCTION;	
** Building location files, with information on distances between zip code centroids and weather monitors, pollution monitors, and traffic sensors;
do `fileloc'/distances_to_zips.do;

** Building traffic data files – traffic data from Caltrans Performance Measurement System website, see individual do-files, text, and read me files for specifics;
** Importing data;
do `fileloc'/importing_hourly_traffic.do;
** Aggregating to weekly level;
do `fileloc'/hourly_to_weekly_traffic.do;
** Converting to zip code level;
do `fileloc'/zip_level_traffic.do;


** Building weather data files – weather data from National Oceanic and Atmospheric Administration website, using Global Surface Summary of the day. See individual do-files, text, and read me files for specifics;
** Importing data;
do `fileloc'/importing_daily_weather.do;
** Aggregating to weekly level;
do `fileloc'/daily_to_weekly_weather;
** Converting to zip code level;
do `fileloc'/zip_level_weather.do;


** Building pollution data files – pollution data from California Air Resources Board website, see individual do-files, text, and read me files for specifics;
** Importing data;
do `fileloc'/importing_daily_pollution.do;
** Aggregating to weekly level;
do `fileloc'/daily_to_weekly_pollution;
** Converting to zip code level;
do `fileloc'/zip_level_pollution.do;

** Building birth data files – birth data from California Department of Public Health Vital Statistics, see individual do-files, text, and read me files for specifics;
** Importing data;
do `fileloc'/importing_daily_birth_data.do;

** Now combine all data files and construct data files used for primary analysis;
** Combine all weekly data files;
do `fileloc'/combine_pollution_weather_traffic.do;
** Construct collapsed weekly hazard data;
do `fileloc'/building_hazard_model.do;

** DATA ANALYSIS;	
** Summary tables (KMS Tables 1, 2, 3);
do `fileloc'/table_birthsum;
do `fileloc'/table_pollutionsum;
** All reduced form tables (Tables 4, 5, 6, 7);
do `fileloc'/reduced_form.do;

** OLS and IV tables (Table 8);
do `fileloc'/regressions_collapsed_OLS;
do `fileloc'/regressions_collapsed_IV;
** IV with varied weather specifications (Table 9)
do `fileloc'/regressions_collapsed_IV_weather_order.do;

** MONTE CARLO ANALYSIS;
** Generate pollution data with errors (Figure 6);
do `fileloc'/fakepollution.do;

** Perform Monte Carlo Analysis (data and Figure 7);
do `fileloc'/KMS_mcloop.do;

log close master_dofile;
